package com.loong.system.controller.backend;

import com.loong.common.mvc.BaseBackendController;
import com.loong.common.param.BaseIdParam;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.common.validator.group.CommonValidate;
import com.loong.global.dto.AdminDTO;
import com.loong.system.common.backend.param.SystemLetterTemplateParam;
import com.loong.system.common.backend.param.SystemLetterTemplateQueryParam;
import com.loong.system.handler.SystemLetterTemplateHandler;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 处理后台系统发送站内信的controller
 */
@RestController
@RequestMapping("manage/system/letterTemplate")
public class SystemLetterTemplateController extends BaseBackendController {

    @Resource
    private SystemLetterTemplateHandler templateHandler;

    /**
     * 新增系统发送站内信
     * @param param 系统发送站内信参数
     * @return 响应的结果
     */
    @PostMapping("save")
    public Result saveLetterTemplate(@Validated(value = {CommonInsert.class})SystemLetterTemplateParam param){
        param.setId(null);
        AdminDTO adminDTO = super.getAdminDTO();
        param.setSender(adminDTO.getUsername());
        param.setSenderId(adminDTO.getId());
        return handlerExecutor.execute(() -> templateHandler.saveLetterTemplate(param));
    }

    /**
     * 分页查询系统发送站内信
     * @param queryParam 查询参数
     * @return 响应的结果
     */
    @PostMapping("pageQuery/letterTemplateList")
    public Result pageQuery(SystemLetterTemplateQueryParam queryParam) {
        return handlerExecutor.execute(() -> templateHandler.pageQuery(queryParam));
    }

    /**
     * 查询系统发送站内信详情
     * @param param 系统发送站内信id
     * @return 响应的结果
     */
    @PostMapping("find/letterTemplateDetail")
    public Result findLetterTemplateDetail(@Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> templateHandler.findLetterTemplateDetail(param.getId()));
    }

    /**
     * 更新系统发送站内信
     * @param param 系统发送站内信参数
     * @return 响应的结果
     */
    @PostMapping("update")
    public Result updateLetterTemplate(@Validated(value = {CommonUpdate.class})SystemLetterTemplateParam param){
        return handlerExecutor.execute(() -> templateHandler.updateLetterTemplate(param));
    }

    /**
     * 批量删除系统发送站内信
     * @param idList 系统发送站内信id集合
     * @return 响应的结果
     */
    @PostMapping("batchDelete")
    public Result batchDeleteLetterTemplate(@RequestParam("idList") List<Long> idList){
        if (CollectionUtils.isEmpty(idList)) return null;
        return handlerExecutor.execute(() -> templateHandler.batchDeleteLetterTemplate(idList));
    }


}
